//
//  TCInfo.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/11.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCInfo: NSView {
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    
    var text:String? {
        didSet {
            if let t = text {
                self.title.stringValue = t
                self.reload()
            }
        }
    }
    
    var isMute:Bool = false {
        didSet {
            self.volumeView.isMute = isMute
            self.reload()
        }
    }
    var isVolumeTip:Bool = false {
        didSet {
            self.reload()
        }
    }
    
    var isNETTip:Bool = false {
        didSet {
            self.reload()
        }
    }
    
    func reload() {
        self.volumeView.isHidden = self.isVolumeTip||self.isMute ? false:true
        let volWidth = self.isVolumeTip||self.isMute ? 18:0
        self.volumeView.snp.updateConstraints { (make) in
            make.width.equalTo(volWidth)
        }
        self.wifiImageV.isHidden = self.isNETTip ? false:true
        let nWidth = isNETTip ? 18:0
        self.wifiImageV.snp.updateConstraints { (make) in
            make.width.equalTo(nWidth)
        }
    }
    
    func updateConstraint() -> CGFloat {
        let text = self.title.stringValue
        let volumeWidth = (self.isVolumeTip==true || self.isMute==true) ? CGFloat(24):CGFloat(0)
        let netWidth = self.isNETTip ? CGFloat(20):CGFloat(0)
        let width = text.ga_width(font: 12)+volumeWidth+netWidth
        self.snp.updateConstraints { (make) in
            make.width.equalTo(200)
        }
        
        return width
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.wantsLayer = true
        self.layer!.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
        
        self.zh_border(radius: 0, borderColor: nil, borderWidth: 0)
        
        self.addSubview(wifiImageV)
        wifiImageV.snp.makeConstraints { (make) in
            make.width.equalTo(0)
            make.height.equalTo(14)
            make.left.equalTo(2)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(volumeView)
        volumeView.snp.makeConstraints { (make) in
            make.width.equalTo(0)
            make.height.equalTo(18)
            make.left.equalTo(wifiImageV.snp.right).offset(0)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
            make.left.equalTo(volumeView.snp.right).offset(2)
        }
    }
    
    lazy var title: NSLabel = {
        let title = NSLabel.label(title: "...", textColor: NSColor.white, font: NSFont.systemFont(ofSize: 12))
        return title
    }()
    
    lazy var wifiImageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"signal1")!)
        return imageV
    }()
    
    lazy var volumeView: VulameView = {
        let view = VulameView.init(size: CGSize.init(width: 18, height: 18))
        view.setValue(value:1, animate: true)
        return view
    }()
}
